package ca.etsmtl.log430.lab3;

/**
 * This class reads Students from the input file and instantiates the Student
 * objects in the system.
 * 
 * Each line of input is read and expected in the following format
 * 
 * <pre>
 * ABCD70123101 Franklin John LOG LOG120 MAT115 CHM101
 * |            |        |    |   |
 * |            |        |    |   Courses taken by this student
 * |            |        |    Program Affiliation
 * |            |        Student's First Name
 * |            Student's Last Name
 * Student ID
 * </pre>
 * 
 * The etudiantsLOG.txt is provided as an example.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.3, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.3, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.2, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.1, G.A. Lewis, 2001-Jan-25 - There was a bug in ParseStudentText that was
 * causing it not to read the last course into the list of courses taken.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class StudentReader extends LineOfTextFileReader {
	StudentList listOfStudents = new StudentList();

	public StudentReader() {
		listOfStudents = null;
	}

	public StudentReader(String inputFile) {
		listOfStudents = getStudentList(inputFile);
	}

	/**
	 * Parses lines of text that are read from the text file containing student
	 * information.
	 * 
	 * @param lineOfText
	 * @return
	 */
	private Student parseStudentText(String lineOfText) {
		boolean done = false; // Indicates the end of parsing
		String token; // String token parsed from the line of text
		int tokenCount = 0; // Number of tokens parsed
		int frontIndex = 0; // Front index or character position
		int backIndex = 0; // Rear index or character position

		/*
		 * Create a student object to record all of the info parsed from the
		 * line of text
		 */
		Student newStudent = new Student();

		while (!done) {
			backIndex = lineOfText.indexOf(' ', frontIndex);

			if (backIndex == -1) {
				done = true;
				token = lineOfText.substring(frontIndex);
			} else {
				token = lineOfText.substring(frontIndex, backIndex);
			}

			switch (tokenCount) {
			case 0: // Student Social Security Number
				newStudent.setStudentID(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 1: // Student's last name
				newStudent.setLastName(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 2: // Student's First name
				newStudent.setFirstName(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			case 3: // Program affiliation (e.g. LOG)
				newStudent.setProgram(token);
				frontIndex = backIndex + 1;
				tokenCount++;
				break;
			default:
				/*
				 * This is where the courses are added to the student'
				 * courses-taken list within the student object Note that there
				 * are no details other than the course ID that is recorded in
				 * the student's courses-taken list.
				 */
				newStudent.getCoursesTakenList().addCourse(new Course(token));
				frontIndex = backIndex + 1;
				break;
			}
		}
		return (newStudent);
	}

	/**
	 * This method reads a line of text. The line of text is passed to the
	 * parseStudentText method where it is parsed and a Student object is
	 * returned. The Student object is then added to the list. When a null is
	 * read from the Student file the method terminates and returns the list to
	 * the caller.
	 * 
	 * @param inputFile
	 * @return a list of Students if it exists, null otherwise.
	 */
	public StudentList getStudentList(String inputFile) {
		String studentText; // Line of text from the Students.txt file
		boolean done; // End of the file - stop processing

		/*
		 * New student list object - this will contain all of the students in
		 * the Students.txt file
		 */
		StudentList listObject = new StudentList();

		if (openFile(inputFile)) {
			done = false;

			while (!done) {
				try {
					studentText = readLineOfText();

					if (studentText == null) {
						done = true;
					} else {
						listObject.addStudent(parseStudentText(studentText));
					}
				} catch (Exception e) {
					return (null);
				}
			}
		} else {
			return (null);
		}
		return (listObject);
	}

	public StudentList getListOfStudents() {
		return listOfStudents;
	}
}